<?php

class NodeController extends Controller {

    /**
     * @var CActiveRecord the currently loaded data model instance.
     */
    private $_model;
    public $activeMenu = 'cms';

    public function actions() {
        //select type

        return array(
            'index' => array(
                'class' => 'NodeIndexAction',
            )
        );
    }

    public function actionCreate($type) {
        $model = new Node;
        $model->node_type = $type;

        if (isset($_POST['Node'])) {
            $model->attributes = $_POST['Node'];
            //save images
            if (($fileUpload = CUploadedFile::getInstanceByName('content_image'))) {
                if (($uploadFileName = UploadHelper::saveContent($fileUpload))) {
                    $model->node_images = $uploadFileName;
                }
            }
            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'New post created successfully');
                //redirect to update
                $this->redirect(array('update', 'id' => $model->node_id));
            }
        }

        $this->pageTitle = 'Create New ' . ucfirst($type);

        $this->render('//object/node/create', compact('model', 'type'));
    }

    public function actionUpdate() {

        $model = $this->loadModel();

        if (isset($_POST['Node'])) {
            $model->attributes = $_POST['Node'];
            //save images
            if (($fileUpload = CUploadedFile::getInstanceByName('content_image'))) {
                if (($uploadFileName = UploadHelper::saveContent($fileUpload))) {
                    $model->node_images = $uploadFileName;
                }
            }
            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'Update post successfully');
            }
        }

        $this->pageTitle = 'Update ' . ucfirst($model->node_type);
        $this->render('/object/node/update', array(
            'model' => $model,
        ));
    }

    public function actionDelete() {
        // we only allow deletion via POST request
        $this->loadModel()->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

        if (!isset($_GET['ajax']))
            $this->redirect(array('index', 'type' => $_GET['type']));
    }

    /**

     * Returns the data model based on the primary key given in the GET variable.

     * If the data model is not found, an HTTP exception will be raised.

     */
    public function loadModel() {

        if ($this->_model === null) {

            if (isset($_GET['id'])) {

                if (Yii::app()->user->isGuest)
                    $condition = 'status=' . Node::STATUS_PUBLISHED . ' OR status=' . Node::STATUS_ARCHIVED;
                else
                    $condition = '';

                $this->_model = Node::model()->findByPk($_GET['id'], $condition);
            }

            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $this->_model;
    }

}